import request from '@/utils/request'
import { rmToken, setToken } from '@/utils/storage'
import { LOGIN_LOGOUT, LOGIN_START } from '../constants'
export const loginStartAc = (token) => ({
  type: LOGIN_START,
  token,
})

export const loginStart = ({ mobile, code }) => {
  return async (dispatch) => {
    /* request.post('/authorizations', {
      mobile,
      code,
    }) */
    const { data } = await request({
      method: 'POST',
      url: '/authorizations',
      data: {
        mobile,
        code,
      },
    })
    const token = data.data.token
    // 持久化到本地
    setToken(token)
    dispatch(loginStartAc(token))
  }
}

// 返回退出的 Action，高度 Reducer 请求 Redux 中的 Token
// 清除本地的 Token
export const loginLogout = () => {
  rmToken()
  return {
    type: LOGIN_LOGOUT,
  }
}

/* export const loginLogout = () => {
  return (dispatch) => {
    rmToken()
    dispatch({
      type: LOGIN_LOGOUT,
    })
  }
} */
